<?php

namespace App\Http\Controllers;


use App\Models\AdminUser;
use App\Models\Category;
use App\Models\Product;
use App\Models\ProductDetail;
use App\Models\Resource;
use Illuminate\Http\Request;
use Ramsey\Uuid\Uuid;

class BatchUploadController extends Controller
{
    public function postUploads(Request $request)
    {
        $data = $request->all();

        $product = new Product;

        $product->uuid = Uuid::uuid1()->toString();

        $adminUser = AdminUser::select('id')->first();
        $product->admin_id = $adminUser ? $adminUser->id : 0;

        $product->name = $data['name'];
        $product->summary = $data['summary'];

        $coverArr = explode(',', $data['cover']);

        $product->cover = $coverArr;
        $product->introduce = $data['introduce'];
        $product->author = $data['author'];
        $product->author_about = $data['author_about'];
        $product->press = $data['press'];

        $categoryIds = [];

        $category = Category::select('id')->where('name', $data['category_id'])->first();

        $product->category_id = 0;
        if ($category) {
            $categoryIds[] = $category->id;
            $product->category_id = $category->id;
        }

        $categoryStr = explode(',', $data['categorys']);
        $categoryArr = Category::select('id')->whereIn('name', $categoryStr)->get()->toArray();
        $categoryArrIds = array_merge($categoryIds, array_pluck($categoryArr, 'id'));

        $resources = explode(',', $data['resources']);
        $resourceArr = Resource::select('id')->whereIn('name', $resources)->get()->toArray();
        $resourceIds = array_pluck($resourceArr, 'id');

        $pictureArr = explode(',', $data['pictures']);

        $product->pictures = $pictureArr;
        $product->buy_link = $data['buy_link'];
        $product->remark = $data['remark'];

        $product->save();

        $product->categorys()->attach($categoryArrIds);
        $product->resources()->attach($resourceIds);


        $details = $data['details'];

        $productDetail = new ProductDetail;

        $productDetail->product_id = $product->id;
        $productDetail->grade = $details['grade'];
        $productDetail->pages = $details['pages'];
        $productDetail->date = $details['date'];
        $productDetail->isbn = $details['isbn'];
        $productDetail->package = $details['package'];
        $productDetail->format = $details['format'];
        $productDetail->material = $details['material'];
        $productDetail->priced = $details['priced'];
        $productDetail->save();

        return json_encode(true);
    }

    /**
     *
     * 后台文章图片上传
     * @param  Request  $request
     * @return Response
     */
    public function uploader(Request $request)
    {
        $file = $request->file('image');

        // 图片上传是否成功
        if ($file->isValid()) {
            // 扩展名
            $ext = $file->getClientOriginalExtension();
            // 生成唯一的文件名
            $filename = date('YmdHis') . uniqid() . '.' . $ext;
            // 将文件存储到 public/images
            $file->move(public_path('uploads/images/'), $filename);
            $path = url('/').'/uploads/images/'. $filename;
            return response()->json(['msg'=>'上传成功','errno'=>0,'data'=>[$path]]);
        }else{
            return response()->json(['msg'=>'上传失败','errno'=>1,'data'=>'']);
        }
    }

}